package lab7;

import java.util.Scanner;

public class Minesweaper {

	private int size, mineCounter;
	private boolean[][] mines;
	private char[][] grid;
	
	public Minesweaper(int size) {
		super();
		this.size = size;
		mineCounter=0;
		mines=new boolean[size][size];
		grid=new char[size][size];
		for (int i = 0; i < size; i++)
			for (int j = 0; j < size; j++)
			{
				if (Math.random()>0.9) 
				{
					mines[i][j]=true;
					mineCounter++;
				}
				grid[i][j]=' ';
			}
	}
	private void dead()
	{
		System.out.println("You hit a mine. You are currently very much dead. Congratulations!");
	}
	
	private void win()
	{
		System.out.println("Congratulations! You have cleared the minefield and are not dead. Unbelievable!");
	}
	
	private void play()
	{
		display();
		choose();
		check();
	}
	
	private void check()
	{
		int count=0;
		for (int i = 0; i<size; i ++)
			for (int j = 0; j<size; j++)
			{
				if (grid[i][j]=='X')
				{
					display();
					dead();
					return;
				}
				if ((grid[i][j]!=' ')&&(grid[i][j]!=' ')) count++;
			}
		if (size*size-count==mineCounter)
		{
			display();
			win();
		}
		play();
	}
	
	private void display()
	{
		System.out.print("  ");
		for (int i = 0; i < size; i++) System.out.print(i+" ");
		System.out.println();
		for (int i = 0; i < size; i++)
		{
			System.out.print(i + " ");
			for (int j = 0; j < size; j++) System.out.print(grid[i][j]+" ");
			System.out.println();
		}
	}
	
	private void choose()
	{
		System.out.println("Please choose a cell:");
		Scanner sc = new Scanner(System.in);
		String choice = sc.nextLine();
		if (choice.length()<3) badChoice();
		int x=0, y=0;
		if ((choice.charAt(0)>='0')&&(choice.charAt(0)<='9')) x=choice.charAt(0)-'0';
		else badChoice();
		if ((choice.charAt(2)>='0')&&(choice.charAt(2)<='9')) y=choice.charAt(2)-'0';
		else badChoice();
		if ((choice.length()>=4)&&(choice.charAt(4)=='F')) flag(x,y);
		else open(x,y);
	}
	
	private void badChoice()
	{
		System.out.print("Invalid choice.");
	}
	
	private void open(int i, int j)
	{
		if (mines[i][j]==true) 
		{
			grid[i][j]='X';
			return;
		}
		if (grid[i][j]!=' ') return;
		int counter=0;
		if ((i>0)&&(j>0)&&(mines[i-1][j-1])) counter++;
		if ((i>0)&&(mines[i-1][j])) counter++;
		if ((j>0)&&(mines[i][j-1])) counter++;
		if ((i<size-1)&&(j<size-1)&&(mines[i+1][j+1])) counter++;
		if ((i<size-1)&&(mines[i+1][j])) counter++;
		if ((j<size-1)&&(mines[i][j+1])) counter++;
		if ((i>0)&&(j<size-1)&&(mines[i-1][j+1])) counter++;
		if ((i<size-1)&&(j>0)&&(mines[i+1][j-1])) counter++;
		grid[i][j]=(char) ('0'+counter);
		if (counter==0)
		{
			if ((i>0)&&(j>0)) open(i-1,j-1);
			if (i>0) open(i-1,j);
			if (j>0) open(i,j-1);
			if ((i<size-1)&&(j<size-1)) open(i+1,j+1);
			if (i<size-1) open(i+1,j);
			if (j<size-1) open(i,j+1);
			if ((i>0)&&(j<size-1)) open(i-1,j+1);
			if ((i<size-1)&&(j>0)) open(i+1,j-1);
		}
	}

	private void flag(int i, int j)
	{
		if (grid [i][j]==' ') grid[i][j]='F';
		else if (grid [i][j]=='F') grid[i][j]=' ';
	}


	public static void main(String[] args) {
		Minesweaper game = new Minesweaper(10);
		game.play();

	}

}
